package com.platform.wechat.controller;


import com.platform.wechat.common.Constant;
import com.platform.wechat.common.JsonBean;
import com.platform.wechat.common.Tools;
import com.platform.wechat.common.WechatAPI;
import com.platform.wechat.entity.Wechat114Msg;
import com.platform.wechat.service.Wechat114Service;
import org.dom4j.DocumentException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

/**
 * @author CX
 * @create 2021-07-08 16:25
 */
@RestController
public class WeChat114Controller {

    @Autowired
    private Wechat114Service wechat114Service;

    /** Logger实例 */
    static final Logger logger = LoggerFactory.getLogger(WeChat114Controller.class);

    /**
     * 开发者填写请求路径，完成微信接入
     * @param wxMsgCheck
     * @return
     */
    @GetMapping("index")
    public String getMsg(Wechat114Msg wxMsgCheck){
        String str = Tools.sortStr(wxMsgCheck.getTimestamp(), wxMsgCheck.getNonce(), WechatAPI.TOKEN);
        String sha1Str = Tools.getSha1Str(str);
        if(sha1Str.equals(wxMsgCheck.getSignature())){
            return wxMsgCheck.getEchostr();
        }
        return "";
    }

    /**
     * 接受用户对公众号的操作，开发者填写url
     * @param request
     * @return
     */
    @PostMapping("index")
    public String postMsg(HttpServletRequest request){
        String resultXml = "";
        try {
            Map<String, String> map = Tools.xmlToMap(request);
            logger.debug(map.toString());
            resultXml = wechat114Service.handleWxMsg(map);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return resultXml;
    }

    @RequestMapping("oAuthGetUserInfo")
    public JsonBean getUserInfoByoAuth(String code, HttpSession session){
        Object wxUserInfo = session.getAttribute("wxUserInfo");
        JsonBean jsonBean = new JsonBean(0, "ok", wxUserInfo);
        if(wxUserInfo == null){
            jsonBean = wechat114Service.getUserInfoByCode(code, session);
        }
        return jsonBean;
    }

    @RequestMapping("shareData")
    public JsonBean shareData(String targetUrl){
        //System.err.println("页面URL:"+targetUrl);
        return wechat114Service.getShareData(targetUrl);
    }

    @PostMapping("oAuth")
    public JsonBean oAuthAndJsConfig(String code){

        return wechat114Service.oAuth(code);
    }

    @PostMapping("jsConfig")
    public JsonBean jsConfig(String url){

        return wechat114Service.jsConfig(url);
    }

    /**
     * 距离计算测试
     * 实际可以只接受一个前端传入的x,y坐标，去后端数据库取出医院，计算距离
     * @param blongitude
     * @param blatitude
     * @param ulongitude
     * @param ulatitude
     * @return
     */
    @PostMapping("calculateDistance")
    public JsonBean calculateDistance(double blongitude, double blatitude, double ulongitude, double ulatitude){
        double distance = Constant.getDistance(blongitude, blatitude, ulongitude, ulatitude);
        return new JsonBean(0, "", distance);
    }
}


